<?php
/**
 *
 *
 * @author mattevigo
 *
 * @project Sandro del Pistoia
 * @created 19/mar/2009
 */
require_once("DBEntity.php");

class Gallery extends DBEntity
{
	const TABLE = 'gallery_galleries';
	const PRIMARY_KEY = 'gallery_id';
	const DATE_FORMAT ="j/m/Y, H:i.s";

	/**
	 *
	 * @return
	 * @param object $db_wrapper
	 * @param object $id[optional]
	 */
	public function __construct(DB $db_wrapper, $id=null)
	{
		parent::__construct($db_wrapper, self::TABLE, self::PRIMARY_KEY, $id);
	}

	/**
	 * Overreading di getJSON, questa funzione oltre che formattare il formato della data
	 * collega ad ogni galleria le foto in essa contenute.
	 * E' inoltre possibile inserire i parametri per visualizzare parte dell'insieme delle
	 * foto.
	 *
	 * @param int $from
	 */
	public function getJSON($from=0, $limit=0)
	{
		$primary_key = self::PRIMARY_KEY;
		$pk = parent::get($primary_key);

	//	$date = date(self::DATE_FORMAT, $this->get('gallery_date'));
	//	$modified = date(self::DATE_FORMAT, $this->get('gallery_modified'));

		$date = $this->get('gallery_date');
		$modified = $this->get('gallery_modified');

		$json = "{\n\t" .
				"\"gallery_id\":\"".$this->get('gallery_id')."\",\n\t" .
				"\"gallery_owner\":\"".$this->getOwner()."\",\n\t" .
				"\"gallery_title\":\"".$this->get('gallery_title')."\",\n\t" .
				"\"gallery_description\":\"".nl2br($this->get('gallery_description'))."\",\n\t" .
				"\"gallery_date\":\"".$date."\",\n\t" .
				"\"gallery_modified\":\"".$modified."\",\n\t";

		//devo aggiungere l'insieme delle foto
		$mysql_query =  "SELECT * " .
						"FROM gallery_photos " .
						"WHERE $primary_key = $pk ";

		if($limit>0)
			$mysql_query .= "ORDER BY photo_id ASC LIMIT $from , $limit";

		$result = $this->db_wrapper->query($mysql_query);

		$json_items = "\"gallery_items\":\n\t\t[";

		$row = mysql_fetch_assoc($result);

		//$count = 0;
		while($row)
		{
			$json_items .= "{\n\t\t\t";
			$i = 0;
			foreach($row as $key => $value)
			{
				if(strcmp($key, 'photo_date') == 0)
					$value = date("j/m/Y", $value);

				$json_items .= "\"$key\":\"".str_replace("\n", "", nl2br($value))."\"";
				$i++;
				if($i<count($row)) $json_items.= ",\n\t\t\t";
			}
			$json_items .= "\n\t\t}";
			if($row = mysql_fetch_assoc($result))
				$json_items .= ",";

			//$count++;
		}
		$json_items .= "]";

		$json .= "\"gallery_size\":\"".$this->getSize()."\",\n\t" .
				$json_items;

		return $json."\n}";
	}

	/**
	 * @return int l'identificatore unico del possessore di questa foto (chiave primaria per gallery_users)
	 */
	private function getUserId()
	{
		return $this->values['user_id'];
	}

	/**
	 * @return int il numero di foto associate a questa galleria
	 */
	public function getSize()
	{
		$table = self::TABLE;
		$id = $this->get('gallery_id');

		$mysql_query = 	"SELECT count(*) " .
						"FROM $table g, gallery_photos p " .
						"WHERE p.gallery_id=g.gallery_id AND g.gallery_id=$id";

		$result = $this->db_wrapper->query($mysql_query);

		$row = mysql_fetch_row($result);

		return $row[0];
	}

	/**
	 * @return string l'username del possessore di questa Photo
	 */
	public function getOwner()
	{
		$table = self::TABLE;
		$table_users = $this->db_wrapper->t_users;

		$mysql_query = "SELECT u.user_username
			FROM $table g, $table_users u
			WHERE g.user_id = {$this->getUserId()} AND g.user_id = u.user_id";

		$result = $this->db_wrapper->query($mysql_query);

		$row = mysql_fetch_row($result);

		return $row[0];
	}

	/**
	 *
	 * @return
	 * @param object $db_wrapper
	 * @param object $id
	 */
	public static function getGalleryFromId($db_wrapper, $id)
	{
		return new Gallery($db_wrapper, $id);
	}

	/**
	 *
	 * @return un array contenente l'id ed il nome delle gallerie
	 * 			'gallery_id' 'gallery_title'
	 * @param object $db_wrapper
	 * @param object $uid
	 */
	public static function getGalleryTitleList($db_wrapper, $uid)
	{
		$table = self::TABLE;

		//interrogazione mysql
		$mysql_query = "SELECT `gallery_id` , `gallery_title` " .
						"FROM `{$db_wrapper->name()}`.`$table` " .
						"WHERE `user_id`='$uid'";

		$result = $db_wrapper->query($mysql_query);
		//$names = mysql_fetch_array($result);
		$names = null;

		$i = 0;
		$names = array();
		while($row = mysql_fetch_array($result))
		{
			$names[$i]['gallery_id'] = $row['gallery_id'];
			$names[$i]['gallery_title'] = $row['gallery_title'];
			$i++;
		}

		return $names;
	}
}

class CoverGallery extends Gallery
{
	const COVER_GALLERY_ID = 100;

	public function __construct(DB $db_wrapper)
	{
		parent::__constructor($db_wrapper, self::COVER_GALLERY_ID);
	}

	public static function getRandomCoverFilename(DB $db_wrapper)
	{
		$gallery_id = self::COVER_GALLERY_ID;

		$mysql_query = 	"SELECT p.photo_filename " .
						"FROM gallery_galleries g, gallery_photos p " .
						"WHERE g.gallery_id=p.gallery_id AND g.gallery_id=$gallery_id";

		$result = $db_wrapper->query($mysql_query);
		$gallery_size = mysql_num_rows($result);

		$random_index = rand(0, $gallery_size-1);

		$i = 0;
		while($row = mysql_fetch_array($result))
		{
			if($i == $random_index)
				return $row['photo_filename'];
			else $i++;
		}
	}
}
?>
